package org.dromara.app.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.bean.domain.*;
import org.dromara.bean.domain.bo.HisPatientBo;
import org.dromara.bean.domain.bo.SearchBo;
import org.dromara.bean.domain.vo.HisDepVo;
import org.dromara.bean.domain.vo.HisDoctorVo;
import org.dromara.bean.domain.vo.HisGoodsVo;
import org.dromara.bean.domain.vo.HisPatientVo;
import org.dromara.bean.mapper.*;
import org.dromara.bean.service.IHisDepService;
import org.dromara.bean.service.IHisDoctorService;
import org.dromara.bean.service.IHisGoodsService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 搜索
 *
 * @author Lion Li
 * @date 2024-12-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@SaIgnore
@RequestMapping("/app/Search")
public class AHisSearchController extends BaseController {

    private final HisDoctorMapper doctorMapper;

    private final HisDepMapper depMapper;

    private final HisGoodsMapper goodsMapper;

    private final SysOssMapper sysOssMapper;

    /**
     * 获取医生科室商品信息
     */
    @GetMapping("/list")
    public R<Object> list(SearchBo bo, PageQuery pageQuery) {
        HashMap<String, Object> map = new HashMap<>();

        LambdaQueryWrapper<HisDep> depWrapper = new LambdaQueryWrapper<>();
        depWrapper.like(HisDep::getTitle, bo.getName());
        depWrapper.eq(HisDep::getDelFlag, 0);
        depWrapper.orderByDesc(HisDep::getCreateTime);
        depWrapper.last("limit 5");
        List<HisDep> hisDeps = depMapper.selectList(depWrapper);
        map.put("deps", hisDeps);


        // 查询所有符合条件的医生
        LambdaQueryWrapper<HisDoctor> doctorWrapper = new LambdaQueryWrapper<>();
        doctorWrapper.like(HisDoctor::getName, bo.getName());
        doctorWrapper.eq(HisDoctor::getDelFlag, 0);
        doctorWrapper.orderByDesc(HisDoctor::getCreateTime);
        doctorWrapper.last("limit 5");
        List<HisDoctor> hisDoctors = doctorMapper.selectList(doctorWrapper);
        hisDoctors.forEach(doctor -> {
            SysOss oss = sysOssMapper.selectById(doctor.getPhoto());
            doctor.setPhotoUrl(oss != null ? oss.getUrl() : null);
        });
        // 查询属于特定部门的医生

        List<Long> depIds = hisDeps.stream().map(HisDep::getId).collect(Collectors.toList());
        List<HisDoctor> hisDepDoctors = new ArrayList<>();
        if (!depIds.isEmpty()) {
            LambdaQueryWrapper<HisDoctor> depDoctorWrapper = new LambdaQueryWrapper<>();
            depDoctorWrapper.eq(HisDoctor::getDelFlag, 0);
            depDoctorWrapper.in(HisDoctor::getDepId, depIds);
            depDoctorWrapper.orderByDesc(HisDoctor::getCreateTime);
            depDoctorWrapper.last("limit 5");
            hisDepDoctors = doctorMapper.selectList(depDoctorWrapper);
        }

        // 合并两个列表并去重
        Set<Long> seenIds = new HashSet<>();
        List<HisDoctor> mergedDoctors = new ArrayList<>();

        // 添加所有符合条件的医生
        for (HisDoctor doctor : hisDoctors) {
            if (seenIds.add(doctor.getId())) {
                mergedDoctors.add(doctor);
            }
        }

        // 添加属于特定部门的医生
        for (HisDoctor doctor : hisDepDoctors) {
            if (seenIds.add(doctor.getId())) {
                mergedDoctors.add(doctor);
            }
        }
        mergedDoctors.forEach(doctor -> {
            HisDep hisDep = depMapper.selectById(doctor.getDepId());
            if (hisDep != null){
                doctor.setTitle(hisDep.getTitle());
            }

        });

        // 将合并后的医生列表放入 map
        map.put("doctors", mergedDoctors);


        LambdaQueryWrapper<HisGoods> goodsWrapper = new LambdaQueryWrapper<>();
        goodsWrapper.like(HisGoods::getTitle, bo.getName());
        goodsWrapper.eq(HisGoods::getDelFlag, 0);
        goodsWrapper.orderByDesc(HisGoods::getCreateTime);
        goodsWrapper.last("limit 5");

        List<HisGoods> cs = goodsMapper.selectList(goodsWrapper);
        cs.forEach(r -> {
            r.setLabel(StringUtils.split(r.getLabels(), ","));
        });
        map.put("goods", cs);


        return R.ok(map);
    }

}
